#include "gameobjectpool.h"
#include "bullet.h"

GameObjectPool* GameObjectPool::instance =nullptr;
GameObjectPool::GameObjectPool(QObject *parent) : QObject(parent)
{

}

void GameObjectPool::Init()
{
    //预先生产对象
    for(int i=0;i<30;i++)
    {
        //子弹生产
        Bullet* bullet =new Bullet();
        mBulletPool.append(bullet);
    }
    for(int i=0;i<13;i++)
    {
        //敌人生产
       EnemyMonster* enemy=new EnemyMonster();
       mEnemyMonsterPool.append(enemy);
    }
}

GameObject *GameObjectPool::GetGameObject(int m_ObgectType)
{
    switch(m_ObgectType)
    {
    case GameObject::OT_BulletPlayer://子弹
    {
        Bullet* bullet =mBulletPool.first();
        mBulletPool.pop_front();
        return bullet;
    }
    case GameObject::OT_EnemyMonster://敌人
    {
        EnemyMonster* enemy =mEnemyMonsterPool.first();
        mEnemyMonsterPool.pop_front();

        return enemy;
    }
    }

}

void GameObjectPool::RecoveryGameObject(GameObject *_object)
{
    switch (_object->GetType())
    {
    case GameObject::OT_BulletPlayer://子弹
    {
        mBulletPool.append((Bullet*)_object);
        break;
    }
    case GameObject::OT_EnemyMonster://敌人
    {
       mEnemyMonsterPool.append((EnemyMonster*)_object);
    }
    }
}

void GameObjectPool::Clear()
{
    //清除子弹容器
    for(auto pBullet:mBulletPool)
    {
        delete pBullet;
    }
    //清除敌人容器
    for(auto pEnemyMonster:mEnemyMonsterPool)
    {
        delete pEnemyMonster;
    }

}

GameObjectPool::~GameObjectPool()
{
    Clear();//内存清除
}
